package com.atguigu.stgy.admin.service.impl;

import com.atguigu.stgy.admin.service.ApartmentLabelService;
import com.atguigu.stgy.model.dto.ApartmentDTO;
import com.atguigu.stgy.model.entity.ApartmentFeeValue;
import com.atguigu.stgy.model.entity.LabelInfo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.stgy.admin.mapper.ApartmentLabelMapper;
import com.atguigu.stgy.model.entity.ApartmentLabel;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApartmentLabelServiceImpl extends ServiceImpl<ApartmentLabelMapper, ApartmentLabel> implements ApartmentLabelService {
    public void batchSaveLabel(ApartmentDTO apartmentDTO, Long apartmentInfoId) {
        if(CollectionUtils.isEmpty(apartmentDTO.getLabelIds())){
            return;
        }
        List<ApartmentLabel> labels = apartmentDTO.getLabelIds().stream()
                .map(labelId -> {
                    ApartmentLabel apartmentLabel = new ApartmentLabel();
                    apartmentLabel.setApartmentId(apartmentInfoId);
                    apartmentLabel.setLabelId(labelId);
                    return apartmentLabel;
                }).collect(Collectors.toList());

        this.saveBatch(labels);
    }

    @Override
    public List<LabelInfo> getLabelInfosByApartmentId(Long apartmentId) {

        return baseMapper.selectLabelInfosByApartmentId(apartmentId);
    }

    @Override
    public void updateApartmentLabelInfos(ApartmentDTO apartmentDTO) {
        this.remove(Wrappers.lambdaQuery(ApartmentLabel.class)
                .eq(ApartmentLabel::getApartmentId , apartmentDTO.getId()));
        //保存本次提交的标签列表
        this.batchSaveLabel(apartmentDTO , apartmentDTO.getId());
    }
}
